//
//  new_progress_timer.h
//  HelloWorld
//
//  Created by Kevin Tang on 30/12/14.
//  Copyright (c) 2014年 Kevin Tang. All rights reserved.
//

#ifndef __HelloWorld__new_progress_timer__
#define __HelloWorld__new_progress_timer__

#include <stdio.h>
#include <boost/progress.hpp>
#include <boost/static_assert.hpp>

template <int N = 2>
class new_progress_timer:public boost::timer, private boost::noncopyable
{
public:
    new_progress_timer( std::ostream & os = std::cout )
    // os is hint; implementation may ignore, particularly in embedded systems
    : timer(), noncopyable(), m_os(os) {
        BOOST_STATIC_ASSERT(N >= 0 && N <= 10);
    }
    ~new_progress_timer()
    {
        //  A) Throwing an exception from a destructor is a Bad Thing.
        //  B) The progress_timer destructor does output which may throw.
        //  C) A progress_timer is usually not critical to the application.
        //  Therefore, wrap the I/O in a try block, catch and ignore all exceptions.
        try
        {
            // use istream instead of ios_base to workaround GNU problem (Greg Chicares)
            std::istream::fmtflags old_flags = m_os.setf( std::istream::fixed,
                                                         std::istream::floatfield );
            std::streamsize old_prec = m_os.precision( N );
            m_os << elapsed() << " s\n" // "s" is System International d'Unites std
            << std::endl;
            m_os.flags( old_flags );
            m_os.precision( old_prec );
        }
        
        catch (...) {} // eat any exceptions
    } // ~progress_timer};
private:
    std::ostream & m_os;
};




#endif /* defined(__HelloWorld__new_progress_timer__) */
